const widgets = require("widget");
const tabs = require("tabs");
var data = require("self").data;
var ss = require("simple-storage");
var request = require('request');
var timer = require('timers');
var disable_minutes = 0;
var username = ss.storage.username;
var md5password = ss.storage.md5password;
var current_location_key = ss.storage.current_location_key;
var configString = data.load("javascript/config.json");
var config = JSON.parse(configString);
config.help_loginFailed = data.load("html/loginFailed.html");
config.help_loginForm = data.load("html/loginFormHelp.html");
config.help_no_decks_found = data.load("html/noDecksFoundHelp.html");
config.help_no_locations_found = data.load("html/noLocationsFoundHelp.html");
config.help_no_cards_found = data.load("html/noCardsFoundHelp.html");
config.loginForm = data.load("html/loginForm.html");
config.changeLocation_html = data.load("html/changeLocation.html");
config.card_html = data.load("html/card.html");
config.username = username;
config.password = md5password;
config.current_location_key = current_location_key;

var panel;
var my_timer;
panel = require("panel").Panel({
	width: 340,
	height: 240,
	allow: {'script': true},
	contentURL: data.url("html/main.html"),
	contentScriptFile: [data.url('javascript/jquery-1.6.2.min.js'),data.url('javascript/jquery-ui-1.8.16.custom.min.js'),data.url('javascript/jquery.tinywatermark-3.1.0.js'),data.url('javascript/astoundlearning.js'),data.url('javascript/jquery.md5.js'),data.url('javascript/TinyDropdown.js')]
});

function show() {
	panel.show();
}
function authenticate(username, md5password) {
	var d = {};
	d.username = username;
	d.md5password = md5password;
	d.current_location_key = current_location_key || '';
	var r = request.Request({
		url: config.baseURL + '/plugin/login.json',
		content: d,
		onComplete: function(response) {
			var json = response.json;
			if (json.login_status == "success") {
				ss.storage.username = username;
				ss.storage.md5password = md5password;
				config.username = username;
				config.md5password = md5password;
				json.config = config;
				panel.port.emit('loginSuccess',json);
				panel.show();
			} else {
				authentication_failed(response);
			}
		}
	});
	r.post();
}

function authentication_failed(response) {
	ss.storage.username = '';
	ss.storage.md5password = '';
	ss.storage.current_location_key = '';
	panel.port.emit('loginFailed',{});
}

panel.port.on('linkClicked', function(json) {
	var href = config.baseURL + json.href;
	require("tabs").open(href);
});

panel.port.on('loginBtnClicked', function(d) {
	authenticate(d.username, d.md5password);
});

panel.port.on('change_location_clicked', function(d) {
	var d = {};
	d.username = config.username;
	d.md5password = config.md5password;

	var r = request.Request({
		url: config.baseURL + '/plugin/locations.json',
		content: d,
		onComplete: function(response) {
			var json = response.json;
			if (json.login_status == "success") {
				panel.port.emit('display_locations',json);
			} else {
				authentication_failed(response);
			}
		}
	});
	r.post();
});

panel.port.on('mark_not_applicable', function(p) {
	var d = {};
	d.username = config.username;
	d.md5password = config.md5password;
	d.entryKey = p.key;
	d.setKey = p.deck_key;

	var r = request.Request({
		url: config.baseURL + '/plugin/mark_not_applicable.json',
		content: d,
		onComplete: function(response) {
			var json = response.json;
			if (json.login_status == "success") {
				panel.port.emit('display_next_card',{});
			} else {
				authentication_failed(response);
			}
		}
	});
	r.post();
});
panel.port.on('load_deck', function(payload) {
	var d = {};
	d.username = config.username;
	d.md5password = config.md5password;
	d.deck_key = payload.key;
	var r = request.Request({
		url: config.baseURL + '/plugin/get_cards_for_deck.json',
		content: d,
		onComplete: function(response) {
			var json = response.json;
			if (json.login_status == "success") {
				panel.port.emit('load_deck_done', json);
			} else {
				authentication_failed(response);
			}
		}
	});
	r.post();
});

panel.port.on('record_entry_displayed', function(p) {
	var d = {};
	d.username = config.username;
	d.md5password = config.md5password;
	d.entryKey = p.key;
	d.setKey = p.deck_key;
	d.view = "study";
	d.shown = p.shown;
	d.wrong = p.wrong;
	var r = request.Request({
		url: config.baseURL + '/plugin/record_entry_displayed.json',
		content: d,
		onComplete: function(response) {
			var json = response.json;
			if (json.login_status == "success") {
			} else {
				authentication_failed(response);
			}
		}
	});
	r.post();
});
panel.port.on('peek', function(p) {
	var d = {};
	d.username = config.username;
	d.md5password = config.md5password;
	d.entryKey = p.key;
	d.setKey = p.deck_key;
	d.shown = p.shown;
	d.wrong = p.wrong;
	var r = request.Request({
		url: config.baseURL + '/plugin/peek.json',
		content: d,
		onComplete: function(response) {
			var json = response.json;
			if (json.login_status == "success") {
			} else {
				authentication_failed(response);
			}
		}
	});
	r.post();
});

panel.port.on('set_location', function(p) {
	ss.storage.current_location_key = p.selected_location_key;
	// invoke authenticate to switch the location effective
	// immediately (we should display cards for this new 
	// location instead of previously selected location)
	authenticate(config.username, config.md5password);
});

panel.port.on('disable', function(p) {
	disable_minutes = p.minutes;
	panel.hide();
});

panel.on('hide', function(p) {
	if (disable_minutes == 0) {
		disable_minutes = 5;
	}
	timer.clearTimeout(my_timer);
	my_timer = timer.setTimeout(show, disable_minutes * 60 * 1000); // we must first convert from minutes to second
});

panel.on('show', function(p) {
	timer.clearTimeout(my_timer);
	my_timer = undefined;
	disable_minutes = 0;
});
var widget = widgets.Widget({
  id: "astoundlearning-plugin",
  label: "AstoundLearning",
  contentURL: data.url("images/bellfavicon.ico"),
  panel: panel,
  allow: {'script': true}
});

panel.port.emit('initialize',{});
if (username) {
	authenticate(username, md5password);
} else {
	panel.port.emit('showLoginForm',config);
}